package com.huanying.goodjob.message;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;

@Service
public class MessageServiceImpl implements MessageService {

	@Autowired
	private BaseDao dao;

	@Override
	public void add_message(Message message) throws Exception {
		// TODO Auto-generated method stub
		dao.save(message);
	
	}
	@Override
	public void update_checkstatus(int id,int status) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Message set checkstatus=? where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(status);
		param.add(id);
		dao.exculeByParams(hql, param);
	}

	@Override
	public void update_message(int id,int status) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Message set status=? where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(status);
		param.add(id);
		dao.exculeByParams(hql, param);
		
	}
	
	@Override
	public void delete_message(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Message set status=0 where id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(id);
	    dao.exculeByParams(hql, param);
	}
	//////////////////////////////////////////////////////////
	@Override
	public void delete_allmessage(int reciveuser_id) throws Exception {
		// TODO Auto-generated method stub
		String hql="update Message set status=0 where reciveuser_id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(reciveuser_id);
	    dao.exculeByParams(hql, param);
	}

	@Override
	public PageBean search_message(Map<String, Object> map, int pageSize, int page) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer hql=new StringBuffer("from Message as m where 1=1");
		List<Object> param = new ArrayList<Object>();
		
		if(map.get("sender")!=null&& map.get("sender")!="")
		{
			hql.append(" and m.sender.name like ?");
			param.add("%"+map.get("sender")+"%");
		}
		if(map.get("receiver")!=null && map.get("receiver")!="")
		{
			hql.append(" and m.receiver.name like ?");
			param.add("%"+map.get("receiver")+"%");
		}
		
		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;

	}

	@Override
	public Message get_message(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="from Message m where m.id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(id);
		
		return (Message)dao.exculeHqlGetOjbectByParams(hql, param);
	}

	@Override
	public List front_get_message(int id) throws Exception {
		// TODO Auto-generated method stub
		String hql="from Message where status=1 and reciveuser_id=?";
		List<Object> param=new ArrayList<Object>();
		param.add(id);
		return dao.exculeHqlByParams(hql, param);
	}

	
	

}
